package com.novel.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtil {

	/**
     * 中文數字转阿拉伯数组【十万九千零六十  --> 109060】
     * @param chineseNumber
     * @return
     */
    public static int chineseNumber2Int(String chineseNumber){
    	
    	String pattern = "(\\D*)(\\d*)(\\D*)";
    	   
        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
   
        // 现在创建 matcher 对象
        Matcher m = r.matcher(chineseNumber);
        if (m.find()) {
        	if (m.group(2).length() > 0) {
        		return Integer.parseInt(m.group(2));
        	}
        }
        chineseNumber = chineseNumber.substring(0,chineseNumber.indexOf("章"));
        
        int result = 0;
        int temp = 1;//存放一个单位的数字如：十万
        int count = 0;//判断是否有chArr
        char[] cnArr = new char[]{'一','二','三','四','五','六','七','八','九'};
        char[] chArr = new char[]{'十','百','千','万','亿'};
        for (int i = 0; i < chineseNumber.length(); i++) {
            boolean b = true;//判断是否是chArr
            char c = chineseNumber.charAt(i);
            for (int j = 0; j < cnArr.length; j++) {//非单位，即数字
                if (c == cnArr[j]) {
                    if(0 != count){//添加下一个单位之前，先把上一个单位值添加到结果中
                        result += temp;
                        temp = 1;
                        count = 0;
                    }
                    // 下标+1，就是对应的值
                    temp = j + 1;
                    b = false;
                    break;
                }
            }
            if(b){//单位{'十','百','千','万','亿'}
                for (int j = 0; j < chArr.length; j++) {
                    if (c == chArr[j]) {
                        switch (j) {
                        case 0:
                            temp *= 10;
                            break;
                        case 1:
                            temp *= 100;
                            break;
                        case 2:
                            temp *= 1000;
                            break;
                        case 3:
                            temp *= 10000;
                            break;
                        case 4:
                            temp *= 100000000;
                            break;
                        default:
                            break;
                        }
                        count++;
                    }
                }
            }
            if (i == chineseNumber.length() - 1) {//遍历到最后一个字符
                result += temp;
            }
        }
        return result;
    }
    public static void main(String[] args) {
		System.out.println(chineseNumber2Int("第二百四十六章我的呼吸突然一窒!"));
	}
}
